package com.kaishengit.web;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kaishengit.pojo.Customer;
import com.kaishengit.service.CustomerService;

@Controller
public class CustomerController {
	private CustomerService customerService;
	
	@RequestMapping("/customerList")
	public String findAll(Model model){
		List<Customer> customerList = customerService.findAll();
		model.addAttribute("customerList", customerList);
		return "customerlist";
	}
	@RequestMapping("/addCustomer")
	public String addCustomer(Customer customer){
		customerService.add(customer);
		return "redirect:/customerList";
	}
	@RequestMapping(value="/editCustomer{id}",method=RequestMethod.GET)
	public @ResponseBody Customer toEditCustomer(@PathVariable String id){
		Customer customer = customerService.findById(id);
		return customer;
	}
	@RequestMapping(value="/editCustomer",method=RequestMethod.POST)
	public String editCustomer(Customer customer){
		customerService.edit(customer);
		return "redirect:/customerList";
	}
	@RequestMapping("/delCustomer{id}")
	public String del(@PathVariable String id){
		customerService.del(id);
		return "redirect:/customerList";
	}
	
	@Autowired
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
}
